<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class navigation_process extends Admin_Controller
{
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('navigation.navigation_list_model');
		$col = $CI->uri->segment($CI->navigation_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->navigation_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->navigation_list_model->cache->add($k, $order[$CI->navigation_list_model->cache->load($k)]);
			} else
				$CI->navigation_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->navigation_list_model->url_prefix.'/'.$CI->navigation_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		
		// model
		$CI->load->module_model('navigation.navigation_list_model');
		
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				$CI->db->delete(NAVIGATION, array(NAVIGATION_MENU_ID=>$id, NAVIGATION_FRANCHISE_ID=>$CI->navigation_list_model->admin_id()));			
			}

			// message
			$CI->navigation_list_model->cache->add($CI->navigation_list_model->msg_cache_prefix, $CI->lang->line('list_delete_all'));
		} else
			// message
			$CI->navigation_list_model->cache->add($CI->navigation_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_delete'));
		
		// go to original page
		redirect($CI->navigation_list_model->url_prefix.'/'.$CI->navigation_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		
		// model
		$CI->load->module_model('navigation.navigation_list_model');
		
		$id = $CI->uri->segment($CI->navigation_list_model->item_segment);
		$CI->db->delete(NAVIGATION, array(NAVIGATION_MENU_ID=>$id, NAVIGATION_FRANCHISE_ID=>$CI->navigation_list_model->admin_id()));
		
		// message
		$CI->navigation_list_model->cache->add($CI->navigation_list_model->msg_cache_prefix, $CI->lang->line('list_delete'));
		
		// go to original page
		redirect($CI->navigation_list_model->url_prefix.'/'.$CI->navigation_list_model->start);
	}	
	
	function saveOrder()
	{
		global $CI;
		
		// model
		$CI->load->module_model('navigation.navigation_list_model');
		
		$new_ordering = array();
		foreach ($_POST as $k=>$v)
		{
			if (ereg("ordering_", $k))
			{
				list($x, $id) = split("_", $k);
				if (in_array($v, array_keys($new_ordering)))
				{
					// message
					$CI->navigation_list_model->cache->add($CI->navigation_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_save_ordering'));
					
					$new_ordering = array();
					
					break;
				} else
					$new_ordering[$v] = array(NAVIGATION_MENU_ID=>$id, NAVIGATION_NAME=>$_POST["name_".$id]);
			}
		}
		
		if (count($new_ordering))
		{
			// save
			foreach ($new_ordering as $id=>$order)			
			{
				$sql = "UPDATE ".NAVIGATION." SET";
				$sql .= " ".NAVIGATION_MENU_ID."=".$id;
				$sql .= " WHERE ".NAVIGATION_FRANCHISE_ID."=".$CI->navigation_list_model->admin_id();
				$sql .= " AND ".NAVIGATION_NAME."=".$CI->db->escape($order[NAVIGATION_NAME]);
				$sql .= " AND ".NAVIGATION_MENU_ID."=".$order[NAVIGATION_MENU_ID];
				$CI->db->query($sql);		
			}
			
			// message
			$CI->navigation_list_model->cache->add($CI->navigation_list_model->msg_cache_prefix, $CI->lang->line('list_save_ordering'));
		}

		// go to original page
		redirect($CI->navigation_list_model->url_prefix.'/'.$CI->navigation_list_model->start);
	}
	
	function saveItem()
	{
		global $CI;
		
		// model
		$CI->load->module_model('navigation.navigation_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        $CI->form_validation->set_rules('name', 'Name', 'trim|required');
		$CI->form_validation->set_rules('pid', 'Page', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{
			// message
			$CI->navigation_detail_model->cache->add($CI->navigation_detail_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : ".$CI->form_validation->error_string());
			$CI->navigation_detail_model->cache->add('post', $_POST);

			// go to original page
			redirect($CI->navigation_detail_model->url_prefix.'/'.$CI->navigation_detail_model->start.'/'.$CI->navigation_detail_model->item);
		}
		
		// save?
		if (!$CI->navigation_detail_model->check())
		{
			// message
			$CI->navigation_detail_model->cache->add('msg', $CI->lang->line('detail_cannot_save')." : ".$CI->lang->line('_name_duplicate'));
			$CI->navigation_detail_model->cache->add('post', $_POST);

			// go to original page
			redirect($CI->navigation_detail_model->url_prefix.'/'.$CI->navigation_detail_model->start.'/'.$CI->navigation_detail_model->item);
		}
		
		if ($CI->navigation_detail_model->hasNavigation())
		{
			// update
			$sql = "UPDATE ".NAVIGATION." SET";
			$sql .= " ".NAVIGATION_NAME."=".$CI->db->escape($CI->input->post('name'));
			$sql .= ", ".NAVIGATION_TOPIC."=".$CI->db->escape($CI->input->post('topic'));
			$sql .= ", ".NAVIGATION_URL."=".$CI->db->escape("?fid=".$CI->navigation_detail_model->admin_id()."&amp;pid=".$CI->input->post('pid'));
			$sql .= ", ".NAVIGATION_PAR_MENU_ID."=".$CI->input->post('par_menu_id');
			$sql .= " WHERE ".NAVIGATION_FRANCHISE_ID."=".$CI->navigation_detail_model->admin_id();
			$sql .= " AND ".NAVIGATION_MENU_ID."=".$CI->navigation_detail_model->item;
			$CI->db->query($sql);
		} else {
			// ordering
			$CI->db->select_max(NAVIGATION_MENU_ID);
			$q = $CI->db->get_where(NAVIGATION, array(NAVIGATION_FRANCHISE_ID=>$CI->navigation_detail_model->admin_id()));
			$row = $q->row();
			
			// insert
			$data = array(
				NAVIGATION_NAME=>$CI->input->post('name'),
				NAVIGATION_TOPIC=>$CI->input->post('topic'),
				NAVIGATION_URL=>"?fid=".$CI->navigation_detail_model->admin_id()."&amp;pid=".$CI->input->post('pid'),
				NAVIGATION_PAR_MENU_ID=>$CI->input->post('par_menu_id'),
				NAVIGATION_FRANCHISE_ID=>$CI->navigation_detail_model->admin_id(),
				NAVIGATION_MENU_ID=>$row?($row->{NAVIGATION_MENU_ID}+1):$CI->navigation_detail_model->item,
			);
			$CI->db->insert(NAVIGATION, $data);
		}

		// message
		$CI->navigation_detail_model->cache->add($CI->navigation_detail_model->msg_cache_prefix, $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->navigation_detail_model->url_prefix2.'/'.$CI->navigation_detail_model->start);
	}		
}

?>